package my.jwds.p2024;

public class MaxConsecutiveAnswers {

    public static void main(String[] args) {

        MaxConsecutiveAnswers obj = new MaxConsecutiveAnswers();
        long start = System.currentTimeMillis();
        System.out.println(obj.maxConsecutiveAnswers("TTFF"
                ,2));
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }



    public int maxConsecutiveAnswers(String answerKey, int k) {
        return Integer.max(maxConsecutiveAnswers(answerKey,k,'F'),maxConsecutiveAnswers(answerKey,k,'T'));
    }


    public int maxConsecutiveAnswers(String answerKey, int k,char c) {
        int max = 0;
        for (int r = 0,l = 0,sum = 0; r < answerKey.length(); r++) {

            sum += answerKey.charAt(r) != c ? 1:0;
            while(sum > k)
                sum -= answerKey.charAt(l++) != c ? 1:0;

            max = Integer.max(r-l+1,max);
        }
        return max;


    }
}